package main.com.oaer.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import main.com.oaer.dao.PurviewDao;
import main.com.oaer.dao.RolePurviewDao;
import main.com.oaer.model.RolePurview;
import main.com.oaer.service.RolePurviewService;

@Service
public class RolePurviewServiceImpl implements RolePurviewService {

	@Autowired
	private RolePurviewDao rolePurviewDao;
	
	@Autowired
	PurviewDao purviewDao;
	
	@Override
	public void add(List<RolePurview> rolePurviewList,Long roleId) {
		for(RolePurview rolePurview:rolePurviewDao.findRolePurviewByRoleId(roleId)){
			rolePurviewDao.delete(rolePurview);
		}	
		for(RolePurview rolePurviewTemp:rolePurviewList){
			rolePurviewDao.add(rolePurviewTemp);
		}
	}

	@Override
	public List<Map<String, Object>> findAllPurviewByRole(Long roleId) {
		List<Map<String, Object>> purviewsList = purviewDao.findPurviews();
		List<RolePurview> rolePurviewList = rolePurviewDao.findRolePurviewByRoleId(roleId);
		for(Map<String, Object> purview:purviewsList){
			for(RolePurview rolePurview:rolePurviewList){
				  if(rolePurview.getPurviewId().equals(purview.get("id"))){
					  purview.put("checked",true);
				  }
			}
		}
		return purviewsList;
	}

}
